const newsService = require("../../service/admin/newsService")

const newsController = {
    add: async (req, res) => {
        console.log(req.file, req.body)
        const cover = req.file ? `/newsuploads/${req.file.filename}` : ''
        const { userId, title, content, category, isPublish } = req.body
        // 调用service进行添加
        await newsService.add({
            userId,
            title,
            content,
            category: Number(category),
            isPublish: Number(isPublish),
            cover,
            editTime: new Date()
        })
        res.send({
            ActionType: 'ok'
        })
    },

    updateList: async (req, res) => {
        console.log(req.file, req.body)
        const cover = req.file ? `/newsuploads/${req.file.filename}` : ''
        const { title, content, category, isPublish, _id } = req.body
        // 调用service进行添加
        await newsService.updateList({
            _id,
            title,
            content,
            category: Number(category),
            isPublish: Number(isPublish),
            cover,
            editTime: new Date()
        })
        res.send({
            ActionType: 'ok'
        })
    },

    getList: async (req, res) => {
        console.log(req.params.id)
        const result = await newsService.getList({ _id: req.params.id })

        res.send({
            ActionType: 'ok',
            data: result
        })
    },
    publish: async (req, res) => {
        console.log(req)
        await newsService.publish({ ...req.body, editTime: new Date })
        res.send({
            ActionType: 'ok'
        })
    },
    delList: async (req, res) => {
        console.log(req.params.id)
        const result = await newsService.delList({ _id: req.params.id })
        console.log(result)
        res.send({
            ActionType: 'ok',
        })
    },
}


module.exports = newsController